package com.beehoo.dcportal.admin.action;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import com.beehoo.dcportal.admin.service.MessageService;
import com.beehoo.dcportal.admin.service.MessageTypeService;
import com.beehoo.dcportal.common.Constant;
import com.beehoo.dcportal.common.base.BaseAction;
import com.beehoo.dcportal.common.base.BaseException;
import com.beehoo.dcportal.model.FileBean;
import com.beehoo.dcportal.model.MenuBean;
import com.beehoo.dcportal.model.MessageBean;
import com.beehoo.dcportal.model.RejectBean;
import com.beehoo.dcportal.utils.CommonUtils;

public class MessageAction extends BaseAction {

	private static final long serialVersionUID = -5495993573897812759L;
	private MessageTypeService messageTypeService;
	private MessageService messageService;
	private List<MenuBean> typeList;
	private List<MenuBean> typeSubList;
	private List<MessageBean> resultList;
	private List<RejectBean> rejectList;
	private MessageBean messageBean;
	private FileBean fileBean;
        private FileBean attachmentFileBean;//附件
	private File filedata;
	private String msgState;
	private String beginDate;
	private String endDate;
	private String type;
	private String subType;
	private String msgId;
	private String reason;
	private String parentId;
	private int totalRows;                   //列表条目总数
	private int pageRows;                    //每页条目数
	private int pageNo;                      //当前页码
	private String naction;                   //请求地址

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getSubType() {
		return subType;
	}

	public void setSubType(String subType) {
		this.subType = subType;
	}

	public List<RejectBean> getRejectList() {
		return rejectList;
	}

	public void setRejectList(List<RejectBean> rejectList) {
		this.rejectList = rejectList;
	}

	public String getParentId() {
		return parentId;
	}

	public void setParentId(String parentId) {
		this.parentId = parentId;
	}

	public List<MenuBean> getTypeSubList() {
		return typeSubList;
	}

	public void setTypeSubList(List<MenuBean> typeSubList) {
		this.typeSubList = typeSubList;
	}

	public String getNaction() {
		return naction;
	}

	public void setNaction(String naction) {
		this.naction = naction;
	}

	public int getTotalRows() {
		return totalRows;
	}

	public void setTotalRows(int totalRows) {
		this.totalRows = totalRows;
	}

	public int getPageRows() {
		return pageRows;
	}

	public void setPageRows(int pageRows) {
		this.pageRows = pageRows;
	}

	public int getPageNo() {
		return pageNo;
	}

	public void setPageNo(int pageNo) {
		this.pageNo = pageNo;
	}

	public void setMessageTypeService(MessageTypeService messageTypeService) {
		this.messageTypeService = messageTypeService;
	}

	public void setMessageService(MessageService messageService) {
		this.messageService = messageService;
	}

	public List<MessageBean> getResultList() {
		return resultList;
	}

	public void setResultList(List<MessageBean> resultList) {
		this.resultList = resultList;
	}

	public MessageBean getMessageBean() {
		return messageBean;
	}

	public void setMessageBean(MessageBean messageBean) {
		this.messageBean = messageBean;
	}

	public List<MenuBean> getTypeList() {
		return typeList;
	}

	public void setTypeList(List<MenuBean> typeList) {
		this.typeList = typeList;
	}

	public String getMsgState() {
		return msgState;
	}

	public void setMsgState(String msgState) {
		this.msgState = msgState;
	}

	public String getBeginDate() {
		return beginDate;
	}

	public void setBeginDate(String beginDate) {
		this.beginDate = beginDate;
	}

	public String getEndDate() {
		return endDate;
	}

	public void setEndDate(String endDate) {
		this.endDate = endDate;
	}

	public String getMsgId() {
		return msgId;
	}

	public void setMsgId(String msgId) {
		this.msgId = msgId;
	}

	public String getReason() {
		return reason;
	}

	public void setReason(String reason) {
		this.reason = reason;
	}

	public FileBean getFileBean() {
		return fileBean;
	}

	public void setFileBean(FileBean fileBean) {
		this.fileBean = fileBean;
	}

	public File getFiledata() {
		return filedata;
	}

	public void setFiledata(File filedata) {
		this.filedata = filedata;
	}

	public String initCreate() {
		String streetId = (String) getSession().get(Constant.SESSION_STREET_ID);
		try {
			typeList = messageTypeService.queryMessageTypeLs(streetId,"1",null,"000");
			if(typeList != null && typeList.size()>0){
				MenuBean menuBean = typeList.get(0);
				typeSubList = messageTypeService.queryMessageTypeLs(streetId, "2", type==null?menuBean.getMenuId():type);
			}
			
		} catch (BaseException e) {
			setMessage(super.getText(e.getMessage()));
		}
		return INPUT;
	}

	public String create() {
		String streetId = (String) getSession().get(Constant.SESSION_STREET_ID);
		if (messageBean != null) {
			messageBean.setStreetId(streetId);
			messageBean.setCoverFile(fileBean);
			messageBean.setCoverFileDir(super.getText("custom.message.cover.dir"));
			messageBean.setCoverFilePath(super.getText("custom.message.cover.path"));
                        messageBean.setAttachmentFile(attachmentFileBean);
			messageBean.setAttachmentFileDir(super.getText("custom.message.attachment.dir"));
			messageBean.setAttachmentFilePath(super.getText("custom.message.attachment.path"));
		}
		try {
			initCreate();
			messageService.createMessage(messageBean);
		} catch (BaseException e) {
			setMessage(super.getText(e.getMessage()));
			return INPUT;
		}
		return list();
	}

	public String list() {
		initDate();
		String streetId = (String) getSession().get(Constant.SESSION_STREET_ID);
		initCreate();
		pageRows = Constant.ADMIN_DEFAULT_ROWS;
		if(pageNo == 0)
			pageNo = 1;
		
		try {
			resultList = messageService.queryMessageForList(streetId, msgState,type,subType,
					beginDate, endDate, pageNo, pageRows);
			totalRows = messageService.queryMessageCount(streetId, msgState,type,subType,
					beginDate, endDate);
			naction = "/admin/message!list.action";
		} catch (BaseException e) {
			setMessage(super.getText(e.getMessage()));
		}
		return "list";
	}

	public String initUpdate() {
		String streetId = (String) getSession().get(Constant.SESSION_STREET_ID);
		try {
			messageBean = messageService.queryMessageDetail(streetId, msgId,
					msgState);
			if(messageBean != null){
				initForUpdate(streetId, messageBean.getMsgType(),msgId,messageBean.getMsgState());
			}
			super.getRequest().setAttribute("method", "update");
		} catch (BaseException e) {
			setMessage(super.getText(e.getMessage()));
		}
		return INPUT;
	}
	
	public String update() {
		String streetId = (String) getSession().get(Constant.SESSION_STREET_ID);
		try {
			if (messageBean != null) {
				messageBean.setStreetId(streetId);
				messageBean.setCoverFile(fileBean);
				messageBean.setCoverFileDir(super.getText("custom.message.cover.dir"));
				messageBean.setCoverFilePath(super.getText("custom.message.cover.path"));
                                messageBean.setAttachmentFile(attachmentFileBean);
                                messageBean.setAttachmentFileDir(super.getText("custom.message.attachment.dir"));
                                messageBean.setAttachmentFilePath(super.getText("custom.message.attachment.path"));
				initForUpdate(streetId, messageBean.getMsgType(),messageBean.getMsgId(),messageBean.getMsgState());
			}
			messageService.updateMessage(messageBean);
		} catch (BaseException e) {
			getRequest().setAttribute("method", "update");
			setMessage(super.getText(e.getMessage()));
			return INPUT;
		}
		return list();
	}

	public String del() {
		try {
			messageService.delMessage(msgId);
		} catch (BaseException e) {
			setMessage(super.getText(e.getMessage()));
		}
		return list();
	}

	public String listAudit() {
		initDate();
		String streetId = (String) getSession().get(Constant.SESSION_STREET_ID);
		if (CommonUtils.nvlToString(msgState).equals("")) {
			msgState = "1";
		}
		
		pageRows = Constant.MSG_DEFAULT_PAGEROWS;
		if(pageNo == 0)
			pageNo = 1;
		
		try {
			resultList = messageService.queryMessageForList(streetId, msgState,type,subType,
					beginDate, endDate, pageNo, pageRows);
			totalRows = messageService.queryMessageCount(streetId, msgState,type,subType,
					beginDate, endDate);
			naction = "/admin/message!listAudit.action";
		} catch (BaseException e) {
			setMessage(super.getText(e.getMessage()));
		}
		return "listAudit";
	}

	public String initAudit() {
		String streetId = (String) getSession().get(Constant.SESSION_STREET_ID);
		try {
			messageBean = messageService.queryMessageDetail(streetId, msgId,
					msgState);
		} catch (BaseException e) {
			setMessage(super.getText(e.getMessage()));
		}
		return "initAudit";
	}

	public String audit() {
		String streetId = (String) getSession().get(Constant.SESSION_STREET_ID);
		String auditorId = (String) getSession().get(Constant.SESSION_USER_ID);
		String auditor = (String) getSession().get(Constant.SESSION_USER_NAME);
		if (messageBean != null) {
			messageBean.setStreetId(streetId);
			messageBean.setAuditorId(auditorId);
			messageBean.setAuditor(auditor);
		}
		try {
			messageService.auditMessage(messageBean, reason);
		} catch (BaseException e) {
			setMessage(super.getText(e.getMessage()));
		}
		return listAudit();
	}
	
	public String upload(){
		FileBean upload = new FileBean();
		upload.setFile(filedata);
		
		String streetId = (String) getSession().get(Constant.SESSION_STREET_ID);
		MessageBean tmpBean = new MessageBean();
		tmpBean.setCoverFile(upload);
		tmpBean.setStreetId(streetId);
		tmpBean.setContentFileDir(super.getText("custom.message.content.dir"));
		tmpBean.setContentFilePath(super.getText("custom.message.content.path"));
		try {
			String fileName = messageService.uploadFile(tmpBean);
			printInfo("", fileName);
		} catch (BaseException e) {
			setMessage(super.getText(e.getMessage()));
		}
		return null;
	}
	
	public String changeType(){
		String streetId = (String) getSession().get(Constant.SESSION_STREET_ID);
		try {
			typeSubList = messageTypeService.queryMessageTypeLs(streetId, "2", parentId);
		} catch (BaseException e) {
			setMessage(super.getText(e.getMessage()));
		}
		return NONE;
	}
	
	private void printInfo(String err, String fileName) throws BaseException{
		getResponse().setCharacterEncoding("utf-8");
		try {
			PrintWriter out = getResponse().getWriter();
			out.println("{\"err\":\"" + err + "\",\"msg\":\"" + getRequest().getContextPath() + fileName + "\"}");		
			out.flush();
			out.close();
		} catch (IOException e) {
			throw new BaseException("custom.messages.error.message.upload.error");
		}
	}

	private void initDate() {
		if(CommonUtils.nvlToString(beginDate).equals(""))
			beginDate = CommonUtils.getPastDay(7);
		if(CommonUtils.nvlToString(endDate).equals(""))
			endDate = CommonUtils.getDate();
	}

	private void initForUpdate(String streetId, String subTypeId, String mid, String msgState)
			throws BaseException {
		MenuBean menuBean = messageTypeService.queryMessageType(streetId,
				subTypeId);
		if (menuBean != null) {
			parentId = CommonUtils.nvlToString(menuBean.getParentMenu());
			typeList = messageTypeService.queryMessageTypeLs(streetId, "1",
					null,"000");
			typeSubList = messageTypeService.queryMessageTypeLs(streetId, "2",
					parentId);
		}
		if(msgState != null && msgState.equals("2")){
			rejectList = messageService.queryRejects(mid);
		}
	}

    /**
     * @return the attachmentFileBean
     */
    public FileBean getAttachmentFileBean() {
        return attachmentFileBean;
    }

    /**
     * @param attachmentFileBean the attachmentFileBean to set
     */
    public void setAttachmentFileBean(FileBean attachmentFileBean) {
        this.attachmentFileBean = attachmentFileBean;
    }
}
